package com.haozhen.streaming

/**
  * @author haozhen
  * @email haozh@ync1.com
  * @date 2021/1/5  21:23
  */
object SocketDemo {

  def main(args: Array[String]): Unit = {
    import org.apache.log4j.{Level, Logger}
    import org.apache.spark.SparkConf
    import org.apache.spark.streaming.{Seconds, StreamingContext}
    Logger.getLogger("org").setLevel(Level.WARN)


   val conf: SparkConf = new SparkConf().setAppName("SocketStream").setMaster("local")
    val ssc = new StreamingContext(conf,Seconds(1))

    //linux 开启 socket nc -lk 9999
    val lines = ssc.socketTextStream("localhost",9999)
    val words = lines.flatMap(_.split("\\s+"))
    val wordCounts = words.map(x=>(x.trim,1)).reduceByKey(_+_)

    wordCounts.print()

    ssc.start()
    ssc.awaitTermination()
  }
}
